package com.ht.medicaltreatment.activity

import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ClickUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.SampleAddAdapetr
import com.ht.medicaltreatment.bean.CentrifugeSampleListBean
import com.ht.medicaltreatment.bean.IceboxItemListBean
import com.ht.medicaltreatment.view.CentrifugeProjectDialog
import com.ht.medicaltreatment.viewmodel.CentrifugeAddSampleViewModel
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_sample_search_layout.btnPositive
import kotlinx.android.synthetic.main.activity_sample_search_layout.btnStart
import kotlinx.android.synthetic.main.activity_sample_search_layout.cbAll
import kotlinx.android.synthetic.main.activity_sample_search_layout.etProjectName
import kotlinx.android.synthetic.main.activity_sample_search_layout.etSampleName
import kotlinx.android.synthetic.main.activity_sample_search_layout.imgProject
import kotlinx.android.synthetic.main.activity_sample_search_layout.recyclerView
import kotlinx.android.synthetic.main.activity_sample_search_layout.tvCheckCount

class SampleSearchAddActivity :BaseActivity(), View.OnClickListener, OnItemClickListener {
    override fun getLayoutId() = R.layout.activity_sample_search_layout

    private val model by viewModels<CentrifugeAddSampleViewModel>()

    private val data:MutableList<IceboxItemListBean> = mutableListOf()
    private var id = ""
    private val adapter = SampleAddAdapetr()
    private var itemAllCount = 0
    private var itemCheckCount = 0
    private var centrifugeId = 0

    override fun init() {
        setTitle("查询添加离心样本")
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        centrifugeId = intent.getIntExtra("centrifugeId",-1)

        adapter.setOnItemClickListener(this)
        btnStart.setOnClickListener(this)
        etProjectName.setOnClickListener(this)
        imgProject.setOnClickListener(this)
        cbAll.setOnClickListener(this)
        btnPositive.setOnClickListener(this)

        ClickUtils.expandClickArea(cbAll,70)
        model.projects {
            data.clear()
            val list = mutableListOf<IceboxItemListBean>()
            it.forEach { bean->
                list.add(IceboxItemListBean(bean.id,bean.protocolNumber))
            }
            data.addAll(list)
        }
    }

    override fun onClick(v: View?) {
        when(v){
            btnStart -> {
                val projectName = etProjectName.text.toString()
                val etSampleName = etSampleName.text.toString()
                if(projectName.isEmpty() || etSampleName.isEmpty()){
                    showToast("请输入完整")
                    return
                }
                showProgress()
                model.findSampleByName(id,etSampleName,{
                    dismissProgress()
                    if(it.size > 0){
                        btnPositive.visibility = View.VISIBLE
                    }
                    itemAllCount = it.size
                    adapter.setNewInstance(it)
                },{
                    dismissProgress()
                    showToast(it)
                })
            }

            etProjectName,
            imgProject -> {
                CentrifugeProjectDialog(this).show(data, {name,id ->
                    etProjectName.text = name
                    this.id = "$id"
                })
            }

            cbAll -> {
                if(cbAll.isChecked){
                    adapter.data.forEach {
                        it.isCheck = true
                    }
                    itemCheckCount = itemAllCount
                }else{
                    adapter.data.forEach {
                        it.isCheck = false
                    }
                    itemCheckCount = 0
                }
                tvCheckCount.text = "$itemCheckCount"
                adapter.notifyDataSetChanged()
            }

            //确认选择
            btnPositive -> {
                if(itemCheckCount == 0){
                    showToast("请选择")
                    return
                }
                showProgress()

                var ids:String? = null
                adapter.data.forEach {
                    if(it.isCheck){
                        if(ids == null){
                            ids = "${it.id}"
                        }else{
                            ids = "$ids,${it.id}"
                        }
                    }
                }
                model.addCentrifuge(ids!!,"$centrifugeId",{
                    dismissProgress()
                    showToast("添加成功")
                    val intent = getIntent()
                    if(it != null){
                        intent.putExtra("centrifugalSpeed",it.centrifugalSpeed)
                        intent.putExtra("centrifugalTemperature",it.centrifugalTemperature)
                        intent.putExtra("centrifugalTime",it.centrifugalTime)
                        setResult(RESULT_OK,intent)
                    }
                    finish()
                },{
                    dismissProgress()
                    showToast(it)
                })
            }
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.data[position] as CentrifugeSampleListBean
        if(item.isCheck){
            item.isCheck = false
            if(cbAll.isChecked){
                cbAll.isChecked = false
            }
            itemCheckCount--
        }else{
            item.isCheck = true
            itemCheckCount++
            if(itemCheckCount == itemAllCount){
                cbAll.isChecked = true
            }
        }
        tvCheckCount.text = "$itemCheckCount"
        adapter.notifyItemChanged(position)
    }
}
